package com.tianqu.tms.www.check.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.check.action.CheckCarPrintCheckAjaxAction;

/**
 * @author zhangxueliang
 * 查验表打印controller
 */
public class CheckCarPrintCheckAjaxController extends TmsController{
	private Logger logger = Logger.getLogger(CheckCarPrintAjaxController.class);
	public static final String TRANSFERACCEPTEDINFO = "TransferAcceptedInfo";
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String serial = (String)uc.getAttribute(CheckCarPrintCheckAjaxAction.SERIAL);
			uc.removeAttribute(CheckCarPrintCheckAjaxAction.SERIAL);
			TransferAcceptedInfo entity = TransferAcceptedInfo.getInstance();
			entity = entity.findPirntNumBySerial(serial);
			TmsUserProfile profile = uc.getUserProfile();
			TransferAcceptedInfo transferAcceptedInfo = new TransferAcceptedInfo();
			transferAcceptedInfo.setSerial(serial);
			transferAcceptedInfo.setCy_printNum(1);
			transferAcceptedInfo.setLc_printNum(entity.getLc_printNum());
			transferAcceptedInfo.setUpdateUserId(profile.getUniqueId());
			transferAcceptedInfo.setCarOwner(entity.getCarOwner());
			transferAcceptedInfo.updateTransferCyPrintNumBySerial(transferAcceptedInfo);
			
			uc.setAttribute(TRANSFERACCEPTEDINFO, entity);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}

}
